$(function () {
    //获取地址栏参数
    var description = getDescription();
    //获取文章内容
    getArtilceDetail(description);
    //获取评论详情
    getArticleComments(description);
})
//获取地址栏参数
function getDescription() {
    var url=window.location.href;
    return url.substring(url.lastIndexOf('/') + 1);
}
//获取文章详情及标题
function getArtilceDetail(description) {
    $.post({
        url: baseurl+api.article.detail+"date/"+description,
        success: function (res) {
            if(res.code=="200"){
                $("#articleid").val(res.data.id);
                var s=" <article class=\"post post-type-normal\" itemscope=\"\" itemtype=\"http://schema.org/Article\"><div class=\"post-block\" style=\"opacity: 1; display: block;\"><header class=\"post-header\" style=\"opacity: 1; display: block; transform: translateY(0px);\">\n" +
                    "                            <h1 class=\"post-title\" itemprop=\"name headline\" >"+res.data.title+"</h1>\n" +
                    "                            <div class=\"post-meta\">\n" +
                    "                                <span class=\"post-time\">\n" +
                    "                                    <span class=\"post-meta-item-icon\">\n" +
                    "                                    <i class=\"fa fa-calendar-day\"></i>\n" +
                    "                                    </span>\n" +
                    "                                    <span class=\"post-meta-item-text\">发表于</span>\n" +
                    "                                    <time title=\"创建于\">"+res.data.time+"</time>\n" +
                    "                                </span>\n" +
                    "                                <span class=\"post-category\">\n" +
                    "                                    <span class=\"post-meta-divider\">|</span>\n" +
                    "                                    <span class=\"post-meta-item-icon\">\n" +
                    "                                        <i class=\"fa fa-folder-open\"></i>\n" +
                    "                                    </span>\n" +
                    "                                    <span class=\"post-meta-item-text\">分类于</span>\n" +
                    "                                    <span itemprop=\"about\" itemscope=\"\" itemtype=\"http://schema.org/Thing\">\n" +
                    "                                        <a  itemprop=\"url\" rel=\"index\">\n" +
                    "                                            <span itemprop=\"name\"><a href='/categories/"+res.data.categoriesName+"'>"+res.data.categoriesName+"</a></span>\n" +
                    "                                        </a>\n" +
                    "                                    </span>\n" +
                    "                                </span>\n" +
                    "                            </div>\n" +
                    "                        </header>\n" +
                    "                        <div class=\"post-body\" itemprop=\"articleBody\" style=\"opacity: 1; display: block; transform: translateY(0px);\">\n" +
                    "\n" +                      marked(res.data.content)
                "                        </div>\n" +
                "                    </div>\n" +
                "                </article>\n" +
                "                <div class=\"post-spread\"></div>";
                $("#posts").html(s);
                navArticle(res.data.pre,res.data.next);
            }else
                window.location.href="/notfound";
        }
    })
}
//导航至上一文章或下一文章
function navArticle(pre,next) {
    var s="";
    if(pre!=null)
        s+="<span style='float: left'><a href='/detail/"+pre.time+"/"+pre.description+"' ><i class=\"fa fa-angle-double-left\">"+pre.title+"</i></a></span>";
    if(next!=null)
        s+="<span style='float: right'><a href='/detail/"+next.time+"/"+next.description+"' >"+next.title+"<i class=\"fa fa-angle-double-right\"></i></a></span>";
    $("#nav-article").html(s);
}
//获取文章下的评论详情
function getArticleComments(description) {
    $.post({
        url: baseurl+api.article.comments+description,
        success: function (res) {
            var s="";
            if(res.code=="401")
                $("#allowflag").val(0);
            else
            if(res.code=="200"){
                $.each(res.data,function (index,comment) {
                    var userHerf=comment.parent.url==""? "<a href='javascript:void(0)' class='comment-size-name'>"+comment.parent.name+" : </a>": "<a href='"+comment.parent.url+"' class=\"comment-size-name\">"+comment.parent.name+" : </a>";
                    s+="   <div class=\"comment-show-con clearfix\">\n" +
                        "                            <div class=\"comment-show-con-img pull-left\"><img src=\"/blog/image/touxiang.png\" class=\"touxaing\">\n" +
                        "                            </div>\n" +
                        "                                <div class=\"comment-show-con-list pull-left clearfix\">\n" +
                        "                                    <div class=\"pl-text clearfix\">\n" +
                        "                                        "+userHerf+"\n" +
                        "                                        <span class=\"my-pl-con\">&nbsp;"+comment.content+"</span>\n" +
                        "                                    </div>\n" +
                        "                                    <div class=\"date-dz\">\n" +
                        "                                        <span class=\"date-dz-left pull-left comment-time\">"+comment.time+"</span>\n" +
                        "                                        <div class=\"date-dz-right pull-right comment-pl-block\">\n" +
                        "                                            <a href=\"javascript:void(0);\" class=\"date-dz-z pull-left\" onclick=\"tocomments("+comment.id+","+comment.parent.id+",'"+comment.parent.name+"')\"><i class=\"date-dz-z-click-red\"></i>回复</a>\n" +
                        "                                        </div>\n" +
                        "                                    </div>";
                    if(comment.child!=null){
                        s+="<div class='hf-list-con' style='display: block;'>";
                        $.each(comment.child,function (index, comment) {
                            var userHerf=comment.user.url==""? "<a href='javascript:void(0);' class=\"atName\">"+comment.user.name+" : </a>": "<a href='"+comment.user.url+"' class=\"atName\">"+comment.user.name+" : </a>";
                            var replyuserHerf=comment.replyUser.url==""? "<a href='javascript:void(0);' class=\"atName\">@"+comment.replyUser.name+" : </a>": "<a href='"+comment.replyUser.url+"' class=\"atName\">@"+comment.replyUser.name+" : </a>";
                            s+="<div class=\"all-pl-con\">\n" +
                                "        <div class=\"pl-text hfpl-text clearfix\">\n" +
                                "        "+userHerf+"\n" +
                                "        <span class=\"my-pl-con\">回复"+replyuserHerf+" :  "+comment.content+"</span>\n" +
                                "        </div>\n" +
                                "        <div class=\"date-dz\">\n" +
                                "        <span class=\"date-dz-left pull-left comment-time\">"+comment.time+"</span>\n" +
                                "        <div class=\"date-dz-right pull-right comment-pl-block\">\n" +
                                "        <a href=\"javascript:;\" class=\"date-dz-z pull-left\" onclick=\"tocomments("+comment.id+","+comment.user.id+",'"+comment.user.name+"')\"><i class=\"date-dz-z-click-red\"></i>回复</a>\n" +
                                "        </div>\n" +
                                "        </div>\n" +
                                "        </div>";

                        });
                        s+="</div></div></div>";
                    }
                })
            }
            $("#comments-box").html(s);
        }
    })
}
//回复评论 更新表单
function tocomments(comentsid,replyuserid,replyusername) {
    $("#comment-content").attr("placeholder","@"+replyusername);
    $("#comment-content").focus();
    $("#lastid").val(comentsid);
    $("#replyuserid").val(replyuserid);
}
function submitcomment() {
  var content=$("#comment-content").val();
  if($("#allowflag").val()==0){
      swal("当前文章不允许评论!");
      return;
  }
  if(content==''){
      swal("请输入要回复的内容!");
      $("#comment-content").focus();
        return;
    }
    checkloginAndDo();
}
function checkloginAndDo() {
    $.post({
        url:adminbaseurl+api.checklogin,
        xhrFields: {
            withCredentials: true
        },
        success:function (res) {
            if(res.code=="200"){
                replyComment();
            }else{
                swal("未登录，请先登录!");
                setTimeout(function () {
                    window.location.href="/login?redirctTo="+window.location.href;
                },1000);
            }
        }
    })
}

function replyComment() {
    $.post({
        url: adminbaseurl+api.article.replycomments,
        data: $("#comments-form").serialize(),
        xhrFields: {
            withCredentials: true
        },
        success: function (res) {
            if (res.code == "200")
                swal(res.message);
            else
                swal(res.message + " 请联系博主!");
            setTimeout(function () {
                window.location.reload();
            }, 1500);
        }
    });
}

